package com.mall.mymall.controller;


import com.mall.mymall.common.Result;
import com.mall.mymall.dao.dto.CommentsDto;
import com.mall.mymall.dao.dto.PageRequestDto;
import com.mall.mymall.dao.dto.QueryDto;
import com.mall.mymall.dao.dto.SubmitCommentsDto;
import com.mall.mymall.dao.dto.querypage.CommentsPageRequest;
import com.mall.mymall.dao.entity.Users;
import com.mall.mymall.service.ProductCommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 十里坡
 * @since 2025-04-01
 */
@RestController
@RequestMapping("/comments")
public class ProductCommentsController {
@Autowired
    ProductCommentsService productCommentsService;
    @PostMapping("/pageMyComments")
    public Result<QueryDto<CommentsDto>> pageMyComments(@RequestBody CommentsPageRequest commentsPageRequest) {
        PageRequestDto pageRequestDto = commentsPageRequest.getPageRequestDto();
        Users users = commentsPageRequest.getUsers();
        QueryDto<CommentsDto> result = productCommentsService.pageMyComments(pageRequestDto, users);
        if (result==null){
            return Result.fail("获取失败");
        }
        return Result.success(result);
    }

    @PostMapping("/pagePendingComments")
    public Result<QueryDto<CommentsDto>> pagePendingComments(@RequestBody CommentsPageRequest commentsPageRequest) {
        PageRequestDto pageRequestDto = commentsPageRequest.getPageRequestDto();
        Users users = commentsPageRequest.getUsers();
        QueryDto<CommentsDto> result = productCommentsService.pagePendingComments(pageRequestDto, users);
        if (result==null){
            return Result.fail("获取失败");
        }
        return Result.success(result);
    }

    @PostMapping("/submitComments")
    public Result<String> submitComments(@RequestBody SubmitCommentsDto submitCommentsDto) {
        boolean result = productCommentsService.saveComments(submitCommentsDto);
        if (result) {
            return Result.success("评论成功");
        }
        return Result.fail("评论失败");
    }

    @DeleteMapping("/delete/{commentId}")
    public Result<String> delete(@PathVariable("commentId") Integer commentId) {
        boolean result = productCommentsService.removeComments(commentId);
        if (result) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }
}

